﻿Public Class Servant

    Dim vAvailability As Boolean
    Dim TempPos(1) As Integer

    Dim MilkingTime As Integer
    Dim CleaningTime As Integer

    'Additional Variable
    Dim Counter As Integer = 0
    Dim MilkedCow As Object


    'true = active
    'false = not active
    Dim LastStatus As Boolean = False
    Dim LastStatus2 As Boolean = False

    Public Property Availability() As Boolean
        Get
            Return vAvailability
        End Get
        Set(ByVal value As Boolean)
            vAvailability = value
        End Set
    End Property

    Private Sub Servant_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackgroundImage = ImageList1.Images(0)
        TempPos(0) = Me.Left
        TempPos(1) = Me.Top
        MilkingTime = 3
        CleaningTime = 5
        Availability = True
    End Sub

    Public Overloads Sub Milking(ByVal ChosenCow As Object)
        Availability = False
        Timer1.Enabled = True
        MilkedCow = ChosenCow
        Me.BringToFront()
        Me.BackgroundImage = ImageList1.Images(1)
        LastStatus = True
    End Sub

    Public Sub StopMilking()
        Timer1.Enabled = False
        Availability = True
        Counter = 0
        Me.Left = TempPos(0)
        Me.Top = TempPos(1)
        Me.BackgroundImage = ImageList1.Images(0)
        LastStatus = False
    End Sub

    Public Overloads Sub Cleaning(ByVal ChosenCow As Object)
        Availability = False
        Timer2.Enabled = True
        MilkedCow = ChosenCow
        Me.BringToFront()
        LastStatus2 = True
    End Sub

    Public Sub StopCleaning()
        Timer2.Enabled = False
        Availability = True
        Counter = 0
        Me.Left = TempPos(0)
        Me.Top = TempPos(1)
        LastStatus2 = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Counter += 1

        If (Counter >= MilkingTime) Then
            MilkedCow.Milking()
            StopMilking()
        End If

    End Sub

    Public Sub Pause(ByVal status As Integer)

        If (LastStatus = True) Then
            Timer1.Enabled = Not status
        End If

        If (LastStatus2 = True) Then
            Timer2.Enabled = Not status
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Counter += 1

        If (Counter >= MilkingTime) Then
            MilkedCow.Cleaning()
            StopCleaning()
        End If
    End Sub
End Class
